package ducct.footprint.ui;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import ducct.footprint.R;
import ducct.footprint.entity.MyFootprint;
import ducct.footprint.utils.Global;

public class CaptureImageActivity extends Activity implements SurfaceHolder.Callback, OnClickListener {
	public static final int FOTO_MODE = 0;
	private final String TAG = getClass().getSimpleName();
	
	private Camera mCamera;
	private boolean mPreviewRunning = false;
	private Context mContext = this;

	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		Log.e(TAG, "onCreate");

		getWindow().setFormat(PixelFormat.TRANSLUCENT);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.capture_image);
		mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
		mSurfaceView.setOnClickListener(this);
		mSurfaceHolder = mSurfaceView.getHolder();
		mSurfaceHolder.addCallback(this);
		mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}

	private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
		public void onPictureTaken(byte[] imageData, Camera c) {
			if (imageData != null) {
				String fileName = storeByteImage(mContext, imageData, 50);
//				mCamera.startPreview();
				
				if (fileName != null) {
					// show UI for creating new foot print
					Intent intent = new Intent(CaptureImageActivity.this, CreateFootprintActivity.class);
					MyFootprint myFootprint = new MyFootprint();
					myFootprint.timestamp = System.currentTimeMillis();
					myFootprint.imagePath = fileName;
					intent.putExtra(Global.MY_FOOTPRINT, myFootprint);
					startActivity(intent);
					
					finish();
				} else {
					// TODO maybe the fileName = null
				}
			}
		}
	};

	protected void onResume() {
		Log.e(TAG, "onResume");
		super.onResume();
	}

	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	protected void onStop() {
		Log.e(TAG, "onStop");
		super.onStop();
	}

	public void surfaceCreated(SurfaceHolder holder) {
		Log.e(TAG, "surfaceCreated");
		mCamera = Camera.open();

	}

	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
		Log.e(TAG, "surfaceChanged");

		// will crash if preview is not running
		if (mPreviewRunning) {
			mCamera.stopPreview();
		}

		Camera.Parameters p = mCamera.getParameters();
		p.setPreviewSize(w, h);
		mCamera.setParameters(p);
		try {
			mCamera.setPreviewDisplay(holder);
		} catch (IOException e) {
			e.printStackTrace();
		}
		mCamera.startPreview();
		mPreviewRunning = true;
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.e(TAG, "surfaceDestroyed");
		mCamera.stopPreview();
		mPreviewRunning = false;
		mCamera.release();
	}

	private SurfaceView mSurfaceView;
	private SurfaceHolder mSurfaceHolder;

	public void onClick(View arg0) {
		mCamera.takePicture(null, mPictureCallback, mPictureCallback);
	}
	
	public String storeByteImage(Context mContext, byte[] imageData, int quality) {
        File sdImageMainDirectory = new File("/sdcard");
		FileOutputStream fileOutputStream = null;
		String fileName = null;
		long currentMiliseconds = System.currentTimeMillis();
		try {
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inSampleSize = 4;
			Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
			
			fileName = sdImageMainDirectory.toString() + "/" + currentMiliseconds + ".png";
			fileOutputStream = new FileOutputStream(fileName);
			BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
			myImage.compress(CompressFormat.PNG, quality, bos);

			bos.flush();
			bos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

		return fileName;
	}

}